package fate.springmvc.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSONObject;

import fate.car.pojo.ACar;
import fate.common.controller.BaseController;
import fate.common.util.ContextUtil;
import fate.common.util.PageInfoUtil;
import fate.rent.pojo.AAdmins;
import fate.rent.pojo.AUsers;
import fate.rent.service.IUsersService;

/**
 * 管理员控制层
 * 
 * @author Administrator
 *
 */

@Controller
public class AUsersController extends BaseController
{
	@Resource( name = "userService")
	private IUsersService iUsersService;

	// 管理员登录功能
	@RequestMapping("/log")
	public String AUsersServlet(Model model,HttpServletRequest request,@RequestParam(value = "name", required = true) String name,
			@RequestParam(value = "password", required = true) String password, HttpSession session)
	{
		/*if("post".equalsIgnoreCase(request.getMethod()))
		{
			try
			{
				name = new String(name.getBytes("ISO8859-1"),"UTF-8");
			} catch (UnsupportedEncodingException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}*/
		Map<String, Object> condMap = new HashMap<String , Object>();
		condMap.put("name", name);
		AUsers aUsers = this.iUsersService.auserLoginService(condMap);
		if(aUsers!=null)
		{
			if(password.equalsIgnoreCase(aUsers.getPassword()))
			{
				String str = "登录成功";
				model.addAttribute(str);
				session.setAttribute("aUsers",aUsers);
				return "/index";
			}else
			{
				String str = "密码错误";
				model.addAttribute(str);
				return "log";
			}
		}
		return null;
	}
	//登录跳转
	@RequestMapping("/getLog")
	public String getLog()
	{
		return "log";
	}
	//退出登录
	@RequestMapping("/out")
	public String out(HttpSession session)
	{
		session.invalidate();
		return "log";
	}
	/*打开客户添加*/
	@RequestMapping("/addusers")
	public String addUsers()
	{
		return "addusers";
	}
	@RequestMapping("/addusersSubmit")
	public String addUsersSubmit(AUsers aUsers,Model model)
	{
		ContextUtil.LOGGER.info("--addUsersSubmit--");
		/* 查询所有启用的角色 */
		JSONObject resultJSON = this.iUsersService.saveListUsersService(aUsers);
		String info = resultJSON.get("info") + "";
		System.out.println("=========" + info);
		/* 存储的结果给放到Request中 */
		model.addAttribute("info", info);
		return "addusers";
	}
	
	/*打开客户查询*/
	@RequestMapping("/queryuser")
	public String queryUsers(String profession,String address,String identity,
			String name,String phone,String sex,String currentPage, String pageSize,Model model)
	{
		/*
		 * 如果不传,默认可是null
		 * */
		Map<String, Object> condMap = new HashMap<String,Object>();
		if(!"".equalsIgnoreCase(profession))
		{
			condMap.put("profession", profession);
		}
		if(!"".equalsIgnoreCase(address))
		{
			condMap.put("address", address);
		}
		if(!"".equalsIgnoreCase(identity))
		{
			condMap.put("identity", identity);
		}
		if(!"".equalsIgnoreCase(name))
		{
			condMap.put("name", name);
		}
		if(!"".equalsIgnoreCase(phone))
		{
			condMap.put("phone", phone);
		}
		if(!"".equalsIgnoreCase(sex))
		{
			condMap.put("sex", sex);
		}
		PageInfoUtil pageInfoUtil = proccedPageInfoUtil(currentPage, pageSize);
		List<AUsers> userList = this.iUsersService.ListUsersService(pageInfoUtil, condMap);
		/* 查询出的数据存储到四大作用域名中 */
		model.addAttribute("userList",userList);
		model.addAttribute("pageInfoUtil",pageInfoUtil);
		return "queryuser";
	}
	//添加用户
	@RequestMapping("/addAdmins")
	public String addAdmins()
	{
		return "addAdmins";
	}
	@RequestMapping("/addadminsSubmit")
	public String addAdminsSubmit(AAdmins aAdmins,Model model)
	{
		ContextUtil.LOGGER.info("--addUsersSubmit--");
		/* 查询所有启用的角色 */
		JSONObject resultJSON = this.iUsersService.saveListAdminsService(aAdmins);
		String info = resultJSON.get("info") + "";
		System.out.println("=========" + info);
		/* 存储的结果给放到Request中 */
		model.addAttribute("info", info);
		return "addAdmins";
	}
	/*打开客户查询*/
	@RequestMapping("/queryadmins")
	public String queryAdmins(String email,String identity,String name,
			String sex,String profession,String currentPage, String pageSize,Model model)
	{
		/*
		 * 如果不传,默认可是null
		 * */
		Map<String, Object> condMap = new HashMap<String,Object>();
		if(!"".equalsIgnoreCase(profession))
		{
			condMap.put("profession", profession);
		}
		if(!"".equalsIgnoreCase(email))
		{
			condMap.put("email", email);
		}
		if(!"".equalsIgnoreCase(identity))
		{
			condMap.put("identity", identity);
		}
		if(!"".equalsIgnoreCase(name))
		{
			condMap.put("name", name);
		}
		if(!"".equalsIgnoreCase(sex))
		{
			condMap.put("sex", sex);
		}
		PageInfoUtil pageInfoUtil = proccedPageInfoUtil(currentPage, pageSize);
		List<AAdmins> adminsList = this.iUsersService.ListAdminsService(pageInfoUtil, condMap);
		/* 查询出的数据存储到四大作用域名中 */
		model.addAttribute("adminsList",adminsList);
		model.addAttribute("pageInfoUtil",pageInfoUtil);
		return "queryadmins";
	}
	//删除用户
	@RequestMapping("/deleteaddmins")
	public String deleteAdmins(String identity)
	{
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("identity", identity);
		int i = this.iUsersService.deleteAdmins(condMap);
		System.out.println("结果：" + i);
		return "queryadmins";
	}
	//删除客户
	@RequestMapping("/deleteusers")
	public String deleteUsers(String identity)
	{
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("identity", identity);
		int i = this.iUsersService.deleteUsers(condMap);
		System.out.println("结果：" + i);
		return "queryuser";
	}
	
}
